package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.mapper.IpLogMapper;
import cn.wolfcode.p2p.base.query.IpLogQueryObject;
import cn.wolfcode.p2p.base.service.IpLogService;
import cn.wolfcode.p2p.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class IpLogServiceImpl implements IpLogService {

    @Autowired
    private IpLogMapper logMapper;

    @Override
    public void save(String username, int state, int userType) {
        IpLog ipLog = new IpLog();
        ipLog.setIp(UserContext.getIp());
        ipLog.setLoginTime(new Date());
        ipLog.setState(state);
        ipLog.setUsername(username);
        ipLog.setUserType(userType);
        logMapper.insert(ipLog);
    }

    @Override
    public PageInfo queryForPage(IpLogQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List list = logMapper.selectForList(qo);
        return new PageInfo(list);
    }
}
